package com.github.cdisk.xtrans.ui;

import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.cdisk.xtrans.core.BaseComponentUI;
import com.github.cdisk.xtrans.ui.event.ComponentMouseListener;

/**
 * 组件UI统计继承基础类
 * @author Cdisk
 *
 */
public class ComponentUI extends BaseComponentUI {

	/** 定义元件绘制时的默认大小 */
	public static final Point SIZE = new Point(32, 32);

	/** 元件UI界面的鼠标事件 */
	@JsonIgnore
	private ComponentMouseListener listener;


	public ComponentUI() {
	}

	public ComponentUI(BaseComponentUI ui) {
		this.x = ui.getX();
		this.y = ui.getY();
		this.selection = ui.isSelection();
	}

	/**
	 * 鼠标事件是否在本控件区域
	 * UI用
	 * @param event
	 * @return
	 */
	public boolean mouseEvent(MouseEvent event) {
		boolean yes = event.x >= this.getX() - ComponentUI.SIZE.x / 2;
		if (!yes) return false;
		yes &= event.x <= this.getX() + ComponentUI.SIZE.x / 2;
		if (!yes) return false;
		yes &= event.y >= this.getY() - ComponentUI.SIZE.y / 2;
		if (!yes) return false;
		yes &= event.y <= this.getY() + ComponentUI.SIZE.y / 2;
		if (!yes) return false;
		return true;
	}

	public ComponentMouseListener getListener() {
		return listener;
	}

	public void setLinstener(ComponentMouseListener listener) {
		this.listener = listener;
	}

}
